/*
 * @Author: xiekai 
 * @Date: 2020-04-29 11:40:25 
 * @Last Modified by: lusx
 * @Last Modified time: 2020-05-09 16:14:22
 */
import {observable,action,toJS,asMap} from 'mobx';
import { SearchPage } from 'weaver-mobile-page';
import { Toast } from "weaver-mobile";
import * as Apis from "../../apis/govern";
import {forwardViewForm} from '../../../workflowForm/util/forwardPage';

export default class CreateProjectStore {
    @observable datas = [];             //所有类型数据
    @observable datasM = [];             //模糊搜索到的类型数据
    @observable loading = false;  //加载列表数据时的loading
    @observable activeKey = [];// 手风琴 展开的key
    @observable dialogSwitch = observable.map ? observable.map({}) : asMap({});    //切换手风琴图标属性
    @observable searchPage = new SearchPage.Store({ route: '/quickbar/new', id: "search" });
    @observable isFuzzy = false;             //是否处于模糊搜索状态       
    @observable searchValue = "";
    searchValue_default = "";

    @action
    changeAccordionIcon = (key, value) => {
        this.activeKey = key;
        let dswitch = observable.map ? observable.map({}) : asMap({});
        key.map(k => dswitch.set(k, value))
        this.dialogSwitch = dswitch;
    }

    //初始化
    @action
    initDatas = (params = {}) => {
        this.loading = true;
        Apis.getCategoryGroup(params).then(result => {
            if (parseInt(result.status) == 1) {
                this.loading = false;
                this.datas = result.data;
            } else {
                Toast.fail(result.error, 1); 
            }
        }).catch(error => {
            Toast.fail(error, 1); 
        });
    }

    @action("修改store中的属性--公共方法")
    setState = (state = {}) => {
        const keys = Object.keys(state);
        keys.forEach(key => {
            if (Array.isArray(state[key])) {
                this[key].replace(state[key]);
            } else {
                this[key] = state[key]
            }
        })
    }

      /**
     * 触发方式（0卡片触发、1触发流程）（key 0新建、1汇报、2分解、3催办、4变更）
     * @param {*} params
     */
    @action
    getActionSetting(categoryid, key) {
        return Apis.getActionSetting({ "categoryid": categoryid, "actiontype": key }).then(data => {
            if (parseInt(data.status) == 1) {
                if (data.isShowModel) {//卡片
                    // _this.showModel(categoryid, key);
                    return data.isShowModel;
                } else {
                    if (data.status == "-1") {
                        Toast.fail(data.error);
                    } else {//流程
                        // let str = data.workflowid.split('&')//workflowid=1119&field29095=139"
                        // let workflowid = str[0].split('=')[1];
                        // let field = {};
                        // field[str[1].split('=')[0]] = str[1].split('=')[1];

                        forwardViewForm('iscreate=1&'+data.workflowid);
                    }
                }
            } else {
                Toast.fail(data.error);
            }
        }).catch(error => {
            Toast.fail(error);
        });
    }
}
